package Day06;

import java.util.*;

public class Main {
    public String solve(String s, String t) {
        int len = Math.max(s.length(), t.length());
        char[] s1 = new char[len];
        char[] s2 = new char[len];
        for(int i = s.length() - 1; i >= 0; i--){
            s1[s.length() - 1 - i] = s.charAt(i);
        }
        for(int i = t.length() - 1; i >= 0; i--){
            s2[t.length() - 1 - i] = t.charAt(i);
        }
        int flag = 0;
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < len; i++){

            int n1 = s1[i] == 0 ? 0 : s1[i] - '0';
            int n2 = s2[i] == 0 ? 0 : s2[i] - '0';
            System.out.println("n1: " + n1);
            System.out.println("n2: " + n2);
            sb.append((n1 + n2 + flag) % 10);
            if(n1 + n2 + flag > 9){
                flag = 1;
            }else{
                flag = 0;
            }
        }
        if(flag == 1){
            sb.append(1);
        }
        return sb.reverse().toString();
    }

    public String solve1 (String s, String t) {
        StringBuilder sb = new StringBuilder(s).reverse();
        StringBuilder tb = new StringBuilder(t).reverse();
        int len = Math.max(sb.length(), tb.length());
        int[] arr = new int[sb.length() + 1];
        for(int i = 0; i < sb.length(); i++){
            for(int j = 0; j < tb.length(); j++){
                arr[i + j] += (sb.charAt(i) - '0') * (tb.charAt(j) - '0');
            }
        }
//        for(int e : arr){
//            System.out.print(e + " ");
//        }
        StringBuilder ans = new StringBuilder();
        int flag = 0;
        for(int i = 0; i < arr.length; i++){
            ans.append((arr[i] + flag)%10);
            flag = (arr[i] + flag) / 10;
        }
        if(flag != 0){
            ans.append(flag);
        }
        String ret = ans.reverse().toString();
        if(ret.charAt(0) == '0'){
            return "0";
        }
        return ret;
    }

    public static void main(String[] args) {
        Main s = new Main();
        System.out.println(s.solve1("123", "99"));
    }
}
